package com.group.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;

import com.base.view.BaseResult;
import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.group.interface1.RequireSuperAdmin;
import com.group.model.*;
import com.group.util.NoteException;

/**
 * 算力和链接领币相关
 * 
 * @author zhaolei
 *
 */
@Controller
public class CapacityController extends BaseController {

	/**
	 * 获取算力链接url
	 * 
	 * @return String
	 */
	@GetMapping("/cap/add")
	@ResponseBody
	@RequireSuperAdmin
	public GeneralResult<String> getCapAddUrl() {
		CapacityUrl cu = new CapacityUrl();
		cu.setState(1);
		beanDao.save(cu);

		UrlMapping um = new UrlMapping();
		// um.setUrl("https://hq.fourtrees.cn/api/cap/redirect?id=" +
		// cu.getId());
		um.setUrl("https://hq.fourtrees.cn/api/cap/redirect?id=" + cu.getId());
		um.setDesc("算力增加链接");
		beanDao.save(um);

		return new GeneralResult<String>().setValue("http://hq.fourtrees.cn/api/weixin/auth/openid?id=" + um.getId());
	}

	/**
	 * 增加算力 (state 0 链接已经使用过) (state 1 算力已达最大值) (state 2 成功)
	 * 
	 * @param request HttpServletRequest
	 * @param id id
	 * @return RedirectView
	 */
	@GetMapping("/cap/redirect")
	public RedirectView capAdd(HttpServletRequest request, Long id) {
		String baseurl = "http://hq.fourtrees.cn/coin/other/count/count.html";
		User user = getUser(request);
		CapacityUrl cu = beanDao.quickQueryOneById(CapacityUrl.class, id);
		if (cu == null)
			throw new NoteException("链接非法");

		if (cu.getState() == 0) {
			return new RedirectView(baseurl + "?state=0");
		}

		if (user.getCapacity() == 1.2) {
			return new RedirectView(baseurl + "?state=1");
		}

		user.setCapacity(user.getCapacity() + 0.2);
		beanDao.save(user);

		cu.setUserId(user.getId());
		cu.setState(0);
		beanDao.save(cu);

		return new RedirectView(baseurl + "?state=2");
	}

	/**
	 * 获得链接(获取币)
	 * 
	 * @param total
	 *            总币数
	 * @param base
	 *            基础币数
	 * @param request HttpServletRequest
	 * @return String
	 */
	@GetMapping("/ticker/share/url")
	@ResponseBody
	public GeneralResult<String> getTickerShareUrl(Double total, Double base, HttpServletRequest request) {
		if (base > total) {
			throw new NoteException("单人领取数量不能大于总数");
		}

		TickerShareUrl tsu = new TickerShareUrl();
		tsu.setBase(base);
		tsu.setReceive(0d);
		tsu.setTotal(total);
		tsu.setUuid(UUID.randomUUID().toString());

		BackUser user = getBackUser(request);
		tsu.setBackUserId(user.getId());

		beanDao.save(tsu);

		String url = "http://hq.fourtrees.cn/api/ticker/share/add?uuid=" + tsu.getUuid();
		UrlMapping um = new UrlMapping();
		um.setUrl(url);
		um.setDesc("自动生成分享的币链接");
		beanDao.save(um);

		tsu.setUrl("http://hq.fourtrees.cn/api/weixin/auth/openid?id=" + um.getId());
		beanDao.save(tsu);

		return new GeneralResult<String>().setValue("http://hq.fourtrees.cn/api/weixin/auth/openid?id=" + um.getId());
	}

	/**
	 * 查询生成的链接
	 * 
	 * @param start 开始
	 * @param limit 条数
	 * @return TickerShareUrl
	 */
	@GetMapping("/ticker/share/url/query")
	@ResponseBody
	public ListResult<TickerShareUrl> queryTickerShareUrl(Integer start, Integer limit) {

		return new ListResult<TickerShareUrl>().setList(tickerDao.queryTickerShareUrl(start, limit)).setTotal(tickerDao.queryTickerShareUrlCount());
	}

	/**
	 * 查询链接领取记录
	 * 
	 * @param tickerShareUrlId tickerShareUrlId
	 * @param start 开始
	 * @param limit 条数
	 * @return TickerShareUrlLog
	 */
	@GetMapping("/ticker/share/url/log/query")
	@ResponseBody
	public ListResult<TickerShareUrlLog> queryTickerShareUrlLog(Long tickerShareUrlId, Integer start, Integer limit) {

		return new ListResult<TickerShareUrlLog>().setList(tickerDao.queryTickerShareUrlLog(tickerShareUrlId, start, limit)).setTotal(tickerDao.queryTickerShareUrlLogCount(tickerShareUrlId));
	}

	/**
	 * 更新链接状态
	 * 
	 * @param tickerShareUrlId tickerShareUrlId
	 * @param state
	 *            1可以用, 0 不肯用
	 * @return BaseResult
	 */
	@PutMapping("/ticker/share/url/state")
	@ResponseBody
	@RequireSuperAdmin
	public BaseResult updateTickerShareUrl(Long tickerShareUrlId, Integer state) {
		TickerShareUrl tsu = new TickerShareUrl();
		tsu.setId(tickerShareUrlId);
		tsu.setState(state);
		beanDao.save(tsu);

		return new BaseResult();
	}

	/**
	 * 点击链接增加币 (state 0 链接失效) (state 1 链接被领完) (state 2 领取成功) (state 3
	 * 已经领过了)ticker:币的数量
	 * 
	 * @param uuid uuid
	 * @param request HttpServletRequest
	 * @return RedirectView
	 */
	@GetMapping("/ticker/share/add")
	public RedirectView addTicker(String uuid, HttpServletRequest request) {
		String baseurl = "http://hq.fourtrees.cn/coin/other/coin/coin.html";
		TickerShareUrl tsu = beanDao.quickQueryOne(TickerShareUrl.class, "uuid", uuid);
		if (tsu == null) {
			throw new NoteException("链接非法");
		}

		if (tsu.getState() == 0) {
			return new RedirectView(baseurl + "?state=0");
		}

		if (tsu.getTotal().doubleValue() == tsu.getReceive().doubleValue()) {
			return new RedirectView(baseurl + "?state=1");
		}

		User user = getUser(request);
		double cap = halfUp(user.getCapacity());
		double ticker = halfUp(cap * tsu.getBase());
		double remain = halfUp(tsu.getTotal() - tsu.getReceive());
		// 不能超出上限
		if (ticker > remain) {
			ticker = remain;
		}

		TickerShareUrlLog log = new TickerShareUrlLog();
		log.setUserId(user.getId());
		log.setTickerShareUrlId(tsu.getId());
		if (beanDao.queryOne(log) != null) {
			return new RedirectView(baseurl + "?state=3");
		}

		log.setUserName(user.getName());
		log.setNum(ticker);
		beanDao.save(log);

		tickerService.addTicker(user.getId(), "wecc", ticker, "二维码领取", null);

		// tsu.setReceive(tsu.getReceive() + ticker);
		// beanDao.save(tsu);
		tickerDao.updateTickerShareUrl(tsu.getId(), ticker);

		return new RedirectView(baseurl + "?state=2&ticker=" + ticker);
	}

}
